var common = require('../../../utils/common.js');
var app=getApp();
var baseUrl=app.url;

Page({
    data: {
        inputShowed: false,
        inputVal: "",
        page:1,
        carList:{},
        searchLoading: false, 
        searchLoadingComplete: false, 
        lable:[['车架号','保养类型','保养日期'],["car_unique_id","style","maintain_time"],"id","car_sn"]
    },
    showInput: function () {
        this.setData({
            inputShowed: true
        });
    },
    seachInput: function () {
        this.setData({carList:{}});
        this.fetchSearchList(1,10,this.data.inputVal);
        
    },
    clearInput: function () {
        this.setData({
            inputVal: "",
            inputShowed:false,
            carList:{}
        });
        this.fetchSearchList(1,10,this.data.inputVal);
    },
    inputTyping: function (e) {
        this.setData({
            inputVal: e.detail.value       
        });
        this.setData({carList:{}});
        this.fetchSearchList(1,10,this.data.inputVal);
    },
    onLoad: function(res){
        this.setData({carList:{}});
        this.fetchSearchList(1,10);  
    }, 
    fetchSearchList: function(page,length,seach=''){
        this.setData({searchLoading:true,searchLoadingComplete: false})
        common.bestCurl('GET','/maintain/index',res=>{
            if(res.statusCode==200){
                if(res.data.code!=2){
                    if(res.data.data.length==0){
                        this.setData({searchLoadingComplete:true,searchLoading:false})
                    }else{
                        if (this.data.carList.length) {
                            this.setData({ carList:  this.data.carList.concat(res.data.data),searchLoading:false})            
                        } else {
                            this.setData({ carList:  res.data.data,searchLoading:false})
                        }                        
                    }
                }else{
                    wx.showToast({
                        title: '无法加载数据',
                        icon: 'loading',
                        duration: 15000
                    });
                }
            }            
        },{'page':page,'length':length,'seach':seach});
    },
    onReachBottom:function (){
        var page = this.data.page;
            page++;
        this.setData({page:page})
        this.fetchSearchList(page,10,this.data.inputVal);
    } 
})
